<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/TooltipSocket.class.php";
require_once dirname(__FILE__)."/TooltipSpecialColor.class.php";

class Tooltip
{	
	static private $instance;
	
	private $special_colors;
	private $sockets;
	
	private $has_set_effect;
	private $yes = 1;
	private $no = 2;
	private $already_done = 3;
	
	private function __construct()
	{
		$this->special_colors = unserialize( file_get_contents( dirname(__FILE__)."/../class.data/Tooltip.special_colors.serialize" ) );
		$this->sockets = unserialize( file_get_contents( dirname(__FILE__)."/../class.data/Tooltip.sockets.serialize" ) );
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new Tooltip();
		}
		
		return self::$instance;
	}
	
	function Flatten($string)
	{
		$string = str_replace("'", "\'", $string);
		$string = str_replace("\n", "\\n", $string);
		$string = str_replace("\"", "'\'", $string);
		return $string;
	}
	
	function MakeWoWTooltip($object_unique_id, $name, $color, $tooltip)
	{		
		$this->has_set_effect = $this->no;
		$string = null;
		
		$string .= "<span style='color: #".$color."; font-size: 10pt;'>".$name."</span><br />";
		
		foreach ($tooltip[0] as $line => $contents)
		{
			if ($contents != $name)
			{
				if ( array_key_exists($line, $tooltip[1]) and strlen($tooltip[1][$line]) > 0 )
				{
					$string .= "<span style='float: right;'>";
					$string .= $this->FormatLine($tooltip[1][$line]);
					$string .= "</span>";
				}
				
				if (strlen($contents) > 0)
				{
					$string .= $this->FormatLine($contents);
					$string .= "<br />";
				}
			}
		}
		
		return $string;
	}
	
	private function FormatLine($line)
	{	
		$line = str_replace("<", "&lt; ", $line);
		$line = str_replace(">", " &gt; ", $line);
		
		$line = preg_replace("/\|c\w{2}(\w{6})([^\|]*)\|r/", "<span style='color: #$1;'>$2</span>", $line);
		
		$t_line = $line;
		
		foreach ($this->special_colors as $tooltip_special_color)
		{	
			$line = preg_replace("/^".$tooltip_special_color->GetMatchRegex()."$/", "<span style='color: #".$tooltip_special_color->GetColor().";'>".$tooltip_special_color->GetDisplayString()."</span>", $line);
			
			if ($t_line != $line and $tooltip_special_color->GetId() == "set_effect" and $this->has_set_effect == $this->no)
			{
				$this->has_set_effect = $this->yes;
			}
			
			if ($t_line != $line)
			{
				break;
			}
		}
		
		if ($t_line == $line)
		{
			foreach ($this->sockets as $tooltip_socket)
			{
				if (preg_match("/^".$tooltip_socket->GetMatchRegex()."$/", $line) > 0)
				{
					$line = "<img src='".$tooltip_socket->GetIcon()."' alt='' style='vertical-align: middle;' /> <span style='color: #".$tooltip_socket->GetColor().";'>".$line."</span>";
				}
			}
		}
		elseif ($this->has_set_effect == $this->yes)
		{
			$line = "<br />".$line;
			$this->has_set_effect = $this->already_done;
		}
		
		return $line;
	}
	
	function MakeWoWTable($contents)
	{
		$string = null;
		
		$string .= "<table style='width: 275px;'>";
		$string .= "<tr>";
		$string .= "<td>";
		
		$string .= "<table cellspacing='0' class='tooltip'>";
		$string .= "<tr>";
		$string .= "<td class='ttip-tl'></td>";
		$string .= "<td class='ttip-t'></td>";
		$string .= "<td class='ttip-tr'></td>";
		$string .= "</tr>";
		$string .= "<tr>";
		$string .= "<td class='ttip-l'></td>";
		$string .= "<td class='ttipbg'>";
		$string .= $contents;
		$string .= "</td>";
		$string .= "<td class='ttip-r'></td>";
		$string .= "</tr>";
		$string .= "<tr>";
		$string .= "<td class='ttip-bl'></td>";
		$string .= "<td class='ttip-b'></td>";
		$string .= "<td class='ttip-br'></td>";
		$string .= "</tr>";
		$string .= "</table>";
		
		$string .= "</td>";
		$string .= "</tr>";
		$string .= "</table>";
		
		return $string;
	}
}

?>